package com.vc.audio;

import android.content.Context;
import android.media.AudioFormat;
import android.util.Log;

import com.vc.core.IAudioEventListener;

public class AudioMain  implements AudioPlayDevCallBack,AudioRecDevCallBack {


	private static final String UITAG = "MVAudio";
	
	//private int volumeLevel = 204;
	
	IAudioEventListener mVcCtrl = null;

	AudioSettingManager AudioModeManger;
	Context _context;
		
	AudioRecorder recDev = null;
	AudioPlayer  playDev = null;
	

	
	long curUser = 0;
	boolean bHandsetMode = true;
	
	public AudioMain(Context context)
	{
		_context = context;

	}
	
	public void SetController(IAudioEventListener vcCtrl)
	{
		Log.d(UITAG, "SetVcController");
		mVcCtrl = vcCtrl;
	}
	
	public void Init(int samplerate,int framelen,int channels,int workmode)
	{
		Log.d(UITAG, "Init");

	    AudioModeManger = new AudioSettingManager(_context);
	    
	    AudioModeManger.SetAudioConfigForCall(true,workmode);
	    
	    AudioModeManger.SetSpeakerOn(true);
	    
	    

	    recDev  = new AudioRecorder();
	    playDev = new AudioPlayer();
	    
	    recDev.SetDevCallBack(this);
	    playDev.SetDevCallBack(this);
	    
	    //TODO 测试用
	    recDev.SetRecFormat(8000<<1, 1 ,30);
	    playDev.SetPlayFormat(8000<<1,1,30);

	    //TODO 正式用
		//recDev.SetRecFormat(samplerate, channels, framelen);
	    //playDev.SetPlayFormat(samplerate,channels,framelen);
	    
	   
	    recDev.Init(_context);
	    playDev.Init(_context);
	}
	
	public void Uninit() 
	{ 
		Log.d(UITAG, "Uninit");
		
		if(playDev != null)
		{
			playDev.Uninit();		
		}
		
		if(recDev != null)
		{
			recDev.Uninit();
		}
		
		playDev = null;
		recDev  = null;
		
		if (AudioModeManger != null)
		{
			AudioModeManger.ResumeAudioConfig();
		}
	}
	   
	
	public void StartSound()
	{
		Log.d(UITAG, "StarSound");
		
		if(playDev != null)
		{
			playDev.StartPlay();		
		}
		
		if(recDev != null)
		{
			recDev.StartRecord();
		}
	}
	
	
	public void StopSound()
	{
		Log.d(UITAG, "StopSound");
		
		if(playDev != null)
		{
			playDev.StopPlay();		
		}
		
		if(recDev != null)
		{
			recDev.StopRecord();
		}
	}
	
	
	public int SetPlayoutSpeaker(boolean loudspeakerOn) {
		if (AudioModeManger != null) {
			AudioModeManger.SetSpeakerOn(loudspeakerOn);
		}
		return 0;
    }

	public int GetPlayoutVolume(int mode) {
		if (AudioModeManger != null) {
			return AudioModeManger.getDeviceCurrentVolume(mode);
		}
		
		return 0;
    }
	
	public void SetPlayoutVolume(int vol, int mode) {
		if (AudioModeManger != null) {
			AudioModeManger.SetSpeakerVolume(vol, mode);
		}
	}
	
	public void SetPlayoutVolume(int vol) {
		if (AudioModeManger != null) {
			AudioModeManger.SetSpeakerVolume(vol, AudioModeManger.getVolumeMode());
		}
	}
	public int GetMaxPlayoutVolume(int mode) {
		if (AudioModeManger != null) {
			return AudioModeManger.getDeviceMaxVolume(mode);
		}
		
		return 0;
	}
	
	public void EnableMicrophoneMute(boolean bEnable) {
		if (AudioModeManger != null) {
			AudioModeManger.SetMicrophoneMute(bEnable);
		}
	}
	
	public int setAudioInputMute(boolean enable) {
		if(recDev != null) {
			recDev.PauseRecord(enable);
			return 0;
		}
		return -1;
	}
	
	public int setAudioOutputMute(boolean enable) {

		/*
		if(playDev != null) {
			return playDev.PlayMute(enable);
		}
		*/
		SetPlayoutVolume(0);
		return -1;
	}
	
	public boolean isHeadsetPluged() {
		if (AudioModeManger != null) {
			return AudioModeManger.isHeadsetPluged(_context);
		}
		
		return false;
	}
	
	public int RecDevDataCallBack(byte[] pcm,int len) {
		if (null != mVcCtrl) {
			return mVcCtrl.recAudioData(pcm, len);
		}

		return -1;
	}

	public int PlayDevDataCallBack(byte[] pcm, int len) {
		if (null != mVcCtrl) {
			return mVcCtrl.playAudioData(pcm, len);
		}
		
		return -1;
	}
	public int getVolumeMode(){

		if(null != AudioModeManger)
			AudioModeManger.getVolumeMode();

		return 0;
	}
}
